package com.eascs.admin.o2o.loan.notify.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.eascs.admin.common.utils.AccountConstants;
import com.eascs.admin.o2o.backFees.entity.O2oBackFeesEntity;
import com.eascs.admin.o2o.backFees.service.O2oBackFeesService;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.notify.O2OMsgNotifyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UuidUtil;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;

/**
 * 个人退款流程
 */
@Service
public class TXMsgNotifyServiceImpl implements O2OMsgNotifyService {
	private static Logger log = LoggerFactory.getLogger(TXMsgNotifyServiceImpl.class);

	
	@Autowired
	O2oBackFeesService o2oBackFeesService;
	 
	@Override
	public ModelResult<O2oLoanapplyEntity> notify(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply){
		ModelResult<O2oLoanapplyEntity> modelResult =new ModelResult<>();
		log.info("启动个人退费通知:[{}]", StatusContexts.WFTYPE_TX);
		log.info("个人退费消息通知:[{}]", notifyParam.getProcessType());
		
		loanapply = txFlow(notifyParam, loanapply);
		modelResult.withModel(loanapply);
		return modelResult;
	}

	@Override
	public List<String> getServiceName() {
		ArrayList<String> list = new ArrayList<String>();
		list.add("ACCOUNT_REFUND");
		return list;
	}
	

	@Override
	public Boolean checkReturn() {
		return Boolean.FALSE;
	}
	
	/**
	 * 个人退款流程
	 * 
	 * @param notifyParam
	 * @param loanapplyEntity
	 * @return
	 */
	@Transactional(transactionManager = "o2oTransactionManager")
	private O2oLoanapplyEntity txFlow(WFNotifyParam notifyParam, O2oLoanapplyEntity loanapply) {
		O2oBackFeesEntity  o2oBackFees = o2oBackFeesService.queryById(notifyParam.getReferId());
		if(o2oBackFees==null){
			log.debug("个人退款流程通过：ReferId:[{}]", notifyParam.getReferId());
		}
		if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_PASS) {
			log.info("个人退款流程通过：ReferId:[{}]", notifyParam.getReferId());
			
			o2oBackFees.setStatus(StatusContexts.WFTYPE_STATUS_CLO);
			int i = o2oBackFeesService.update(o2oBackFees);
			if (i > 0) {
				log.info("ReferId:[{}],更新个人退款表状态成功", notifyParam.getReferId());
			} else {
				log.info("ReferId:[{}],更新个人退款表状态失败", notifyParam.getReferId());
			}
			
			String reulst = "";
			try {
				reulst = o2oBackFeesService.ChargingDJ(o2oBackFees, AccountConstants.ACCOUNT_DJ_ZF);
			} catch (RuntimeException e) {
				reulst = e.getMessage();
			}
			
			if(StringUtil.isNotBlank(reulst)&&reulst.equals("SUCCESS")){
				log.info("个人退款流程通过，调用数据账户冻结转支付成功");
			}else{
				new RuntimeException("个人退款流程通过，调用数据账户冻结转支付失败");
			}
			
		} else if (notifyParam.getProcessStatus() == ProcessTaskMain.PROECESS_STATUS_TURNED_DOWN) {
			log.info("个人退款流程驳回：ReferId:[{}]", notifyParam.getReferId());
			o2oBackFees.setStatus(StatusContexts.BZJ_STATUS_NEW);
			o2oBackFees.setUniqueId(UuidUtil.generateShortUuid());
			
			String reulst = o2oBackFeesService.ChargingDJ(o2oBackFees, AccountConstants.ACCOUNT_JD);
			if(StringUtil.isNotBlank(reulst)&&reulst.equals("SUCCESS")){
				log.info("个人退款流程驳回，回调用数据账户解冻成功");
			}else{
				
				throw new RuntimeException("个人退款驳回调用数据账户解冻失败");
			}
			
			int i = o2oBackFeesService.update(o2oBackFees);
			if (i > 0) {
				log.info("ReferId:[{}],驳回个人退费成功", notifyParam.getReferId());
			} else {
				log.info("ReferId:[{}],驳回个人退费失败", notifyParam.getReferId());
			}
		} else if (notifyParam.getProcessStatus().equals(ProcessTaskMain.PROECESS_STATUS_REJECT)) {
			log.info("个人退费流程作废：ReferId:[{}]", notifyParam.getReferId());
		}
		return loanapply;
	}
	
}
